home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d927.lha / Telnet / src / network.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  4KB  |  167 lines

  1. /*
  2.  * Copyright (c) 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)network.c    5.1 (Berkeley) 9/14/90";
  22. #endif /* not lint */
  23.  
  24. #include <sys/types.h>
  25. #include <string.h>
  26. #include <sys/socket.h>
  27. #include <sys/time.h>
  28. #ifdef AMI_TCP
  29. #include <bsdsocket.h>
  30. #else
  31. #include <ss/socket.h>
  32. #endif
  33.  
  34. #include <errno.h>
  35.  
  36. #include <arpa/telnet.h>
  37.  
  38. #include "ring.h"
  39.  
  40. #include "defines.h"
  41. #include "externs.h"
  42. #include "fdset.h"
  43.  
  44. Ring    netoring, netiring;
  45. char    netobuf[2*BUFSIZ], netibuf[BUFSIZ];
  46.  
  47. /*
  48.  * Initialize internal network data structures.
  49.  */
  50.  
  51. init_network()
  52. {
  53.     if (ring_init(&netoring, netobuf, sizeof netobuf) != 1) {
  54.     exit(1);
  55.     }
  56.     if (ring_init(&netiring, netibuf, sizeof netibuf) != 1) {
  57.     exit(1);
  58.     }
  59.     NetTrace = stdout;
  60. }
  61.  
  62.  
  63. /*
  64.  * Check to see if any out-of-band data exists on a socket (for
  65.  * Telnet "synch" processing).
  66.  */
  67.  
  68. int
  69. stilloob()
  70. {
  71.     static struct timeval timeout = { 0 };
  72.     fd_set    excepts;
  73.     int value;
  74.  
  75.     do {
  76.     FD_ZERO(&excepts);
  77.     FD_SET(net, &excepts);
  78.     value = select(net+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
  79.     } while ((value == -1) && (errno == EINTR));
  80.  
  81.     if (value < 0) {
  82.     perror("select");
  83.     (void) quit();
  84.     /* NOTREACHED */
  85.     }
  86.     if (FD_ISSET(net, &excepts)) {
  87.     return 1;
  88.     } else {
  89.     return 0;
  90.     }
  91. }
  92.  
  93.  
  94. /*
  95.  *  setneturg()
  96.  *
  97.  *    Sets "neturg" to the current location.
  98.  */
  99.  
  100. void
  101. setneturg()
  102. {
  103.     ring_mark(&netoring);
  104. }
  105.  
  106.  
  107. /*
  108.  *  netflush
  109.  *        Send as much data as possible to the network,
  110.  *    handling requests for urgent data.
  111.  *
  112.  *        The return value indicates whether we did any
  113.  *    useful work.
  114.  */
  115.  
  116.  
  117. int
  118. netflush()
  119. {
  120.     register int n, n1;
  121.     extern long bytesout;
  122.  
  123.     if ((n1 = n = ring_full_consecutive(&netoring)) > 0) {
  124.     if (!ring_at_mark(&netoring)) {
  125.         n = send(net, netoring.consume, n, 0);    /* normal write */
  126.     } else {
  127.         /*
  128.          * In 4.2 (and 4.3) systems, there is some question about
  129.          * what byte in a sendOOB operation is the "OOB" data.
  130.          * To make ourselves compatible, we only send ONE byte
  131.          * out of band, the one WE THINK should be OOB (though
  132.          * we really have more the TCP philosophy of urgent data
  133.          * rather than the Unix philosophy of OOB data).
  134.          */
  135.         n = send(net, netoring.consume, 1, MSG_OOB);/* URGENT data */
  136.     }
  137.     }
  138.     if (n < 0) {
  139.     if (errno != ENOBUFS && errno != EWOULDBLOCK) {
  140.         setcommandmode();
  141.         perror(hostname);
  142.         (void)NetClose(net);
  143.         ring_clear_mark(&netoring);
  144.         longjmp(peerdied, -1);
  145.         /*NOTREACHED*/
  146.     }
  147.     n = 0;
  148.     }
  149.     if (netdata && n) {
  150.     Dump('>', netoring.consume, n);
  151.     }
  152.     if (n) {
  153.     bytesout+=n;
  154.     ring_consumed(&netoring, n);
  155.     /*
  156.      * If we sent all, and more to send, then recurse to pick
  157.      * up the other half.
  158.      */
  159.     if ((n1 == n) && ring_full_consecutive(&netoring)) {
  160.         (void) netflush();
  161.     }
  162.     return 1;
  163.     } else {
  164.     return 0;
  165.     }
  166. }
  167.